//登录注册表单组件
<template>
  <div class="bg">
    <div class="login-card">
      <el-form :model="loginForm" ref="loginForm" :rules="rules" label-width="80px">
        <div class="login-form-wrapper">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="tomain">登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
import router from "@/router/router";
import { ElMessage } from 'element-plus'
import { mapState } from 'vuex';
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    ...mapMutations(['setUser']),
    handleSubmit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // 提交登录表单逻辑
          console.log('表单数据：', this.loginForm);
        } else {
          console.error('表单数据验证失败');
          return false;
        }
      });
    },
    tomain() {
      // const flag=document.querySelector('.login-card')
      // flag.remove();
      console.log(this.loginForm);
      console.log(this.user)
      this.$api.userApi.login(this.loginForm)
        .then(res => {
          
          res = res.data
          console.log("---")
          console.log(res.code)
          if (res.code == 200) {
            console.log(res.data);
            this.setUser(res.data.username)
            console.log(this.user)
            ElMessage({
              message: '登录成功😊！',
              type: 'success',
              duration: 1000,
            })
            // this.closeLogin()
            // this.$emit("close")
            
            // localStorage.setItem("token", res.data.token)
            router.push({ path: '/home', replace: true })
          } else {
            ElMessage({
              message: res.msg,
              type: 'error',
              duration: 1000,
            })
          }
        })
        .catch(err => {
          console.log("请求失败")
        })
     
    }
  }
};
</script>
<style scoped>
.login-card {
  width: 400px;
  margin: 0 auto;
  padding: 30px;
  position: relative;
  top: 300px;
  border: 1px solid #e6e6e6;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-color: white;
}

.login-form-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.bg {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  overflow-y: auto;
  background: url("../static/bg.png") no-repeat center center fixed;
  background-size: cover;

}
</style>